<template>
  <div class="login-wrap">
    <el-form
      label-position="left"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="0px"
      class="demo-ruleForm login-container"
    >
      <h3 class="title">用户登录</h3>
      <el-form-item prop="loginInfo">
        <el-input
          type="text"
          v-model="ruleForm.loginInfo"
          auto-complete="off"
          placeholder="请输入用户名或手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" style="padding-top:15px">
        <el-input
          type="password"
          v-model="ruleForm.password"
          auto-complete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="captcha" style="display: inline-block;">
        <el-input
          type="text"
          v-model="ruleForm.captcha"
          auto-complete="off"
          placeholder="验证码"
          style="width: 200px"
        ></el-input>
      
      </el-form-item>
        <img
          @click="createCaptcha"
          :src="postUrl"
          alt=""
          style="
            width: 130px;
            height: 40px;
            border-radius: 5px;
            position: relative;
            top: 15px;
            left: 12px;
          "
        />

      <!-- <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox>
      <el-link type="primary" style="margin-left: 155px" @click="regist"
        >没有账号?去注册</el-link
      > -->
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          style="width: 100%"
          @click="submitForm"
          :loading="logining"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script type="text/ecmascript-6">
import Axios from "axios";
import { dev } from "../../config/index";
const baseUrl = dev.baseUrl;
export default {
  name: "login",
  data() {
    return {
      postUrl: baseUrl + "/createImageCode",
      //定义loading默认为false
      logining: false,
      // 记住密码
      rememberpwd: false,
      ruleForm: {
        //loginInfo和password默认为空
        loginInfo: "", //用户名或手机号
        password: "",
        captcha: "", //  验证码
      },
      //rules前端验证
      rules: {
        loginInfo: [{ required: true, message: "请输入用户名或手机号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  // 创建完毕状态(里面是操作)     sadasdasd
  created() {
    this.$message({
      message: "欢迎来到EXLG-卓越物流管理平台",
      type: "success",
    });
  },
  // 里面的函数只有调用才会执行
  methods: {
    //获取info列表，登录
    submitForm() {
      this.logining = true;
      //调用 ../api/userMG 下的login的api
      Axios({
        method: "post",
        url: `${baseUrl}/employee/empLogin`, //员工登录的请求
        data: {
          loginInfo: this.ruleForm.loginInfo,
          password: this.ruleForm.password,
          code: this.ruleForm.captcha,
        },
      }).then((res) => {
        if (res.data.code === 0) {
          //密码正确登录成功
          this.$router.push({ path: "/charts/statistics" });
          this.logining = false;
          //将用户信息保存到vuex中,供后续访问
          localStorage.setItem("userInfo", JSON.stringify(res.data.emp));
          // this.$store.state.emp = res.data.emp;
        } else if (res.data.code === 501) {
          // 验证码错误
          this.logining = false;
          this.postUrl = baseUrl + "/createImageCode";
          this.ruleForm.captcha = "";
          this.$message({
            message: res.data.msg,
            type: "error",
          });
        } else {
          // 用户名或密码错误
          //登录失败  弹窗提示用户
          this.logining = false;
          (this.ruleForm.loginInfo = ""),
            (this.ruleForm.password = ""),
            (this.ruleForm.captcha = "");
          this.postUrl = baseUrl + "/createImageCode";
          this.$message({
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    regist() {
      this.$router.push({ path: "/regist" });
    },
    createCaptcha() {
      this.postUrl = baseUrl + "/createImageCode?d=" + new Date();
    },
  },
  mounted() {},
  beforeDestroy() {
    this.$bus.off("logout");
  },
  beforeUpdate() {},
  activated() {
    (this.ruleForm.loginInfo = ""),
      (this.ruleForm.password = ""),
      (this.ruleForm.captcha = ""),
      this.createCaptcha();
  },
};
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.codeimg {
  height: 40px;
}
</style>